<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
      <el-form-item label="股票名称" prop="name">
        <el-input
            v-model="queryParams.scokName"
            placeholder="请输入股票名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getList">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="View"
            @click="openMyEntrust"
            v-hasPermi="['system:role:add']"
        >我的委托</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="股票编号" prop="scokId" width="120" />
      <el-table-column label="用户" prop="uname" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="买入股票价格" prop="scokPrice" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="股票名称" prop="scokName" width="100" />
      <el-table-column label="股票数量" prop="num" width="100" />
      <el-table-column label="状态" align="center" width="100">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1">
<!--              @change="handleStatusChange(scope.row)">-->
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="买入委托" placement="top" v-if="scope.row.roleId !== 1">
            <el-button link type="primary" icon="TopRight" @click="openBuy" v-hasPermi="['system:role:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="卖出委托" placement="top" v-if="scope.row.roleId !== 1">
            <el-button link type="primary" icon="BottomLeft" @click="openSell(scope.row)" v-hasPermi="['system:role:edit']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 买入委托 -->
    <el-dialog v-model="dialogFormVisible" title="买入委托" width="500">
      <el-form :model="buy">
        <el-form-item label="股票代码" :label-width="formLabelWidth">
          <el-input v-model="buy.stockId" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="股票名称" :label-width="formLabelWidth">
          <el-select v-model="buy.scokName" placeholder="请选择股票">
            <el-option :value="item.shareName" v-for="item in stockList" :key="item.id" :label="item.shareName" @click="getStockCode(item)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市场最新成交价" :label-width="formLabelWidth">
          <el-input v-model="buy.price" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="买入数量" :label-width="formLabelWidth">
          <el-input v-model="buy.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="买入价格" :label-width="formLabelWidth">
          <el-input v-model="buy.scokPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所需金额" :label-width="formLabelWidth">
          <el-input v-model="scokPriceCount" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">关闭</el-button>
          <el-button type="primary" @click="goAddStock">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 卖出委托 -->
    <el-dialog v-model="dialogFormVisibleSell" title="卖出委托" width="500">
      <el-form :model="sell">
        <el-form-item label="股票代码" :label-width="formLabelWidthSell">
          <el-input v-model="sell.stockId" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="股票名称" :label-width="formLabelWidthSell">
          <el-input v-model="sell.scokName" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="市场最新成交价" :label-width="formLabelWidthSell">
          <el-input v-model="sell.price" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="卖出价格" :label-width="formLabelWidthSell">
          <el-input v-model="sell.scokPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="卖出数量" :label-width="formLabelWidthSell">
          <el-input v-model="sell.num" autocomplete="off" @input="checkSellQuantity"></el-input>
        </el-form-item>
        <el-form-item label="可获金额" :label-width="formLabelWidthSell">
          <el-input v-model="sellPriceCount" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisibleSell = false">关闭</el-button>
          <el-button type="primary" @click="goAddSell">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 我的委托   -->
    <el-dialog v-model="dialogTableVisible" title="我的委托" width="1000">
      <el-table :data="myEntrust">
        <el-table-column property="stockId" label="股票码" width="150"></el-table-column>
        <el-table-column property="scokName" label="股票名称" width="200"></el-table-column>
        <el-table-column label="委托状态">
          <template #default="scope">
            {{ scope.row? (scope.row.status === 0? '正在委托' : '完成') : '' }}
          </template>
        </el-table-column>
        <el-table-column property="num" label="股票数量"></el-table-column>
        <el-table-column property="scokPrice" label="期望价格"></el-table-column>
        <el-table-column property="falg" label="买/卖？">
          <template #default="scope">
            {{ scope.row? (scope.row.falg === 0? '买入' : '卖出') : '' }}
          </template>
        </el-table-column>
        <el-table-column property="uname" label="发布人"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="撤销委托" placement="top" v-if="scope.row.roleId !== 1">
              <el-button link type="primary" icon="User" @click="closeStock(scope.row)" v-hasPermi="['system:role:edit']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup name="Role">
import {listMystock} from "@/api/system/mystock"
import {getBuy, getSell, getPrice, buyStockName,goMyEntrust,closeEntrust} from "../../../api/system/share.js";
/** 买入参数 */
const buy = reactive({
  stockId: '',
  scokName: '',
  price: '',
  num: '',
  scokPrice: '',
  scokPriceCount: '',
})
const scokPriceCount = computed(() => {
  return buy.num * buy.scokPrice
})
const dialogFormVisible = ref(false);
const formLabelWidth = '140px'
//---------------------------------------------------
/** 股票下拉列表参数 */
const stockList = ref([]);
//---------------------------------------------------
/**卖出参数 */
const sell = reactive({
  stockId: '',
  scokName: '',
  price: '',
  num: '',
  scokPrice: '',
  sellPriceCount: '',
  sellNum:''
})
const sellPriceCount = computed(() => {
  return sell.num * sell.scokPrice
})

const dialogFormVisibleSell = ref(false);
const formLabelWidthSell = '140px'
//---------------------------------------------------
/** 我的委托参数 */
const dialogTableVisible = ref(false);
const myEntrust = ref([])
//---------------------------------------------------
const router = useRouter();
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const menuRef = ref(null);
const deptRef = ref(null);

/** 数据范围选项*/
const dataScopeOptions = ref([
  { value: "1", label: "全部数据权限" },
  { value: "2", label: "自定数据权限" },
  { value: "3", label: "本部门数据权限" },
  { value: "4", label: "本部门及以下数据权限" },
  { value: "5", label: "仅本人数据权限" }
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: undefined
  },
  rules: {
    roleName: [{ required: true, message: "角色名称不能为空", trigger: "blur" }],
    roleKey: [{ required: true, message: "权限字符不能为空", trigger: "blur" }],
    roleSort: [{ required: true, message: "角色顺序不能为空", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询角色列表 */
function getList() {
  loading.value = true;
  listMystock(proxy.addDateRange(queryParams.value,dateRange.value)).then(response => {
    dataList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
    console.log("++++++++++++++-----------------------------"+dataList.value);
  });
}
/** 打开买入委托 */
function openBuy(){
  dialogFormVisible.value = true;
  buyStockName().then(res => {
    console.log(res.data);
    stockList.value = res.data
  })
}
/** 获取股票代码及最新价格 */
function getStockCode(item){
  buy.stockId = item.code
  getPrice(buy.stockId).then(response => {
    buy.price = response.data
  })
}
/** 确认买入 */
function goAddStock(){
  buy.scokPriceCount = buy.num * buy.scokPrice
  getBuy(buy).then(response => {
    if (response.msg == 200){
      proxy.$modal.msgSuccess(response.data);
      dialogFormVisible.value = false;
      getList();
    }
  });
}
/** 打开卖出委托 */
function openSell(row){
  dialogFormVisibleSell.value = true;
  sell.stockId = row.scokId
  sell.scokName = row.scokName
  sell.num = row.num
  sell.sellNum =row.num
  getPrice(sell.stockId).then(response => {
    sell.price = response.data
  })
}
/** 检查卖出数量 */
function checkSellQuantity(value){
  if (value > sell.sellNum) {
    alert("卖出数量不能大于持有数量");
    sell.num = sell.sellNum;
  }
}
/** 卖出委托 */
function goAddSell() {
  if (sell.num > sell.sellNum) {
    alert("卖出数量不能大于持有数量");
    return;
  }
  getSell(sell).then(response => {
    if (response.msg == 200){
      proxy.$modal.msgSuccess(response.data);
      dialogFormVisibleSell.value = false;
      getList();
    }
  });
}
/** 查看我的委托 */
function openMyEntrust(){
  dialogTableVisible.value = true;
  goMyEntrust().then(response => {
    myEntrust.value = response.data;
  })
}
/** 撤销委托 */
function closeStock(row){
  console.log(row)
  closeEntrust(row).then(response => {
    if (response.msg == 200){
      proxy.$modal.msgSuccess(response.data);
      goMyEntrust().then(response => {
        myEntrust.value = response.data;
      })
    }
  })
}
getList();
</script>
